package com.cgt.xiaotao.order.controller;

import com.cgt.common.domain.dto.PageDTO;
import com.cgt.common.utils.R;
import com.cgt.xiaotao.order.domain.dto.*;
import com.cgt.xiaotao.order.domain.query.OrderPageQuery;
import com.cgt.xiaotao.order.domain.vo.*;
import com.cgt.xiaotao.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单接口")
@Slf4j
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {

    private final IOrderService orderService;

    @ApiOperation("创建订单快照")
    @PostMapping("/create")
    public R<OrderSnapshot> createOrder(@RequestBody CartItemDTO cartItemDTO) {
        try {
            List<OrderItemDTO> orderItemDTOS = cartItemDTO.getOrderItemDTOS();
            List<Long> couponIds = cartItemDTO.getCouponIds();
            OrderSnapshot orderSnapshot = orderService.createOrder(orderItemDTOS, couponIds);
            return R.ok(orderSnapshot);
        } catch (Exception e) {
            return R.fail(e.toString());
        }
    }

    @ApiOperation("修改订单信息")
    @PutMapping("/update")
    public R<String> updateOrder(@RequestBody OrderRenewDTO dto) {
        try {
            orderService.updateOrder(dto);
            return R.ok("修改成功");
        } catch (Exception e) {
            return R.fail(e.toString());
        }
    }

    @ApiOperation("查询订单")
    @GetMapping("/query")
    public R<OrderVO> queryOrder(@RequestParam String orderNo) {
        try {
            OrderVO orderVO = orderService.queryOrder(orderNo);
            return R.ok(orderVO);
        } catch (Exception e) {
            return R.fail(e.toString());
        }
    }

    @ApiOperation("生成支付快照")
    @PostMapping("/payment")
    public R<PaymentSnapshot> payment(@RequestBody PaymentDTO paymentDTO) {
        try {
            PaymentSnapshot paymentSnapshot = orderService.payment(paymentDTO);
            return R.ok(paymentSnapshot);
        } catch (Exception e) {
            return R.fail(e.toString());
        }
    }

    @ApiOperation("查询支付订单")
    @GetMapping("/payment/{orderNo}")
    public R<PaymentVO> queryPayment(@PathVariable String orderNo) {
        try {
            return R.ok(orderService.queryPayment(orderNo));
        } catch (Exception e) {
            return R.fail(e.toString());
        }
    }

    @ApiOperation("查询订单列表")
    @GetMapping("/list")
    public R<PageDTO<OrderListVO>> selectPage(
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Byte status,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) Boolean isAsc) {
        try {
            OrderPageQuery query = new OrderPageQuery();
            if (pageNo != null) {
                query.setPageNo(pageNo);
            }
            if (pageSize != null) {
                query.setPageSize(pageSize);
            }
            query.setStatus(status);
            query.setSortBy(sortBy);
            query.setIsAsc(isAsc);

            return R.ok(orderService.selectPage(query));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("查询订单详情-我的订单页面")
    @GetMapping("/getOrderItem")
    public R<OrderItemVO> getOrderItem(@RequestParam String orderNo) {
        try {
            return R.ok(orderService.getOrderItem(orderNo));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
